const app = getApp();
const Controller = require('../controllers/controllers.js').controller;
const baseUrl = require('../../pages/controllers/controllers').baseUrl;
// pages/introduce/edit.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //案件类型
    type:0,   //1 律师介绍 2 擅长案例类型 3.案例 4.相关资质
    value:'',
    number:0,
    maxLength:200,
    value1:'',
    number1:'0',
    maxLength1:20,
    typeList:[],
    currentSelect:[],
    lawyerInfo:{},
    qualificationImgUrls:[]
  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let type=options.type
    this.setData({
      type
    })   
 //获取律师信息
  this.getlawyerInfo()
//获取案件类型列表
  this.getCategoryList()
  },
  getlawyerInfo(){
    Controller.lawyerinfo().then(res=>{
      let type=this.data.type
      let baseData=res.data.baseData
      if(type==1){
       let value=baseData.desc
       let number=value.length||0
        this.setData({
          value,
          number
        })
      }
      else if(type==2){
     let  currentSelect=baseData.goodsCategoryIds.split(',').map((item)=>{
          return Number(item)
        })
      this.setData({
         currentSelect
      })
      console.log('currentSelect',this.data.currentSelect)
      }
      else if(type==3){
        let value=baseData.caseContent
        let value1=baseData.caseTitle
        let number=value.length||0
        let number1=value1.length||0
        this.setData({
          value,value1,number,number1
        })
      }
      else if(type==4){
      
        if(baseData.qualificationImgUrls){
          let qualificationImgUrls=baseData.qualificationImgUrls.split(',')
       this.setData({
        qualificationImgUrls
       })
        }else{
          this.setData({
            qualificationImgUrls:['','']
           })
        }
        
      
     }
      
    })
  },
  //获取案件类型列表
  getCategoryList(){
    Controller.categoryList({}).then(res=>{
      let typeList=res.data.baseData
      this.setData({
         typeList
      })
    })
  },
  focus(e){
   console.log('e==>',e)
  },
  
 //输入文本
  handleInput(e){


   let value=e.detail.value

   let maxLength=this.data.maxLength
   
   let len=value.length

   this.setData({
     value
   })

   if(len>maxLength){
      return
   }

   this.setData({
     number:len
   })
     
   
   
  },
//输入标题
  inputCaseTitle(e){
    console.log('执行')
    let value1=e.detail.value
    let maxLength1=this.data.maxLength1
    let len=value1.length
    this.setData({
      value1
    })
    if(len>maxLength1){
      return
    }
    this.setData({
         number1:len      
    })
  },
  //改变当前选项的索引值
  changeIndex(e){
    console.log('id',e.target.dataset.id)
     let id=e.target.dataset.id
     let list=this.data.currentSelect
     let index
     if((index=list.indexOf(id))>-1){
         list.splice(index,1)
     }else{
         list.push(id)
     }
        this.setData({
          currentSelect:list
        })  
  },
  //更新律师信息
  lawyerupdateInfo(){
    let type=this.data.type
    let obj={}
    if(type==1){
     obj.desc=this.data.value
    }
    else if(type==2){
     obj.goodsCategoryIds=this.data.currentSelect.join(',')
    }else if(type==3){
     obj.caseTitle=this.data.value1
     obj.caseContent=this.data.value
    }else if(type==4){
     obj.qualificationImgUrls=''
     
     this.data.qualificationImgUrls.map((item,index)=>{
       if(item){
         obj.qualificationImgUrls+=item+','
       }
     })
     if(!obj.qualificationImgUrls){
        wx.showToast({
          title: '内容能为空',
          icon:'none'
        })
     }
     obj.qualificationImgUrls=obj.qualificationImgUrls.slice(0,-1)
    
    }else{
      return
    }
 console.log('obj==>',obj)
    for(let key in obj){
     if(obj[key].trim()==''){
       return wx.showToast({
         title:'内容不能为空',
         icon:'none'
       })
     }
    }
    Controller.lawyerupdateInfo(obj).then(res=>{
       wx.showToast({
         title: '编辑成功',
         icon:'none'
       })
       wx.navigateBack({
         delta: -1,
       })
    }).catch(err=>{
       
    })
  },
  //上传资质图片
  uploadFile(e){
   let that=this
    let index=e.currentTarget.dataset.index
    wx.chooseImage({
      count: 1,
      sizeType: ['original'],
      sourceType: ['album'],
      success (res) {
        // tempFilePath可以作为img标签的src属性显示图片
        const tempFilePaths = res.tempFilePaths
        let img=tempFilePaths[0]
       
      //上传图片
      wx.uploadFile({
        url: baseUrl+'/upload/picture',
        filePath: img,
        name: "file",
        success: function (res) {
        console.log('succeess',res)
        let img=JSON.parse(res.data).data.baseData[0]
      
         let qualificationImgUrls=that.data.qualificationImgUrls
         qualificationImgUrls[index]=img
       
          that.setData({
            qualificationImgUrls
          })
          
        
        }
    })
       
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})